<template>
  <div class="home">
    <Navbar />
    <div class="content">
          <div class="main">
            <div class="fish-list">
              <div
                v-for="fish in seafishData"
                :key="fish.id"
                class="fish-item"
                @click="goToFishDetails(fish.id)"
              >
                <img :src="fish.image" :alt="fish.name" class="fish-image" />
                <div class="fish-info">
                  <span class="fish-name">{{ fish.name }}</span>&nbsp;&nbsp;&nbsp;&nbsp;
                  <span class="fish-user">
                    用户:
                    {{ fish.user }}</span>
                </div>
              </div>
          </div>
        </div>
        
        <div class="pagination">
          <button @click="prevPage" :disabled="currentPage === 1">返回</button>
          <span>页数 {{ currentPage }} 到 {{ totalPages }}</span>
          <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
          <div>
            <label for="goToPage">到某页:</label>
            <input
              id="goToPage"
              type="number"
              v-model.number="goToPageNumber"
              min="1"
              :max="totalPages"
            />
            <button
              @click="goToPage"
              :disabled="
                !goToPageNumber || goToPageNumber < 1 || goToPageNumber > totalPages
              "
            >
              Go
            </button>
          </div>
        </div>
    </div>
    <Footer />
  </div>
</template>

<script setup>
import Navbar from "../components/Navbar.vue";
import Footer from "../components/Footer.vue";
import { ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import request from '../api/request'

const seafishData = ref([])
const currentPage = ref(1)
const totalPages = ref(1)
const goToPageNumber = ref(null)
const router = useRouter()
const route = useRoute() 

const fetchData = async () => {
  try {
    const type = route.params.type
    const response = await request.get('seafish_api.php', {
      params: {
        type,
        page: currentPage.value
      }
    })

    seafishData.value = response.data.data
    totalPages.value = Math.ceil(response.data.total_count / response.data.items_per_page)
  } catch (error) {
    console.error(error)
  }
}

const prevPage = () => {
  if (currentPage.value > 1) {
    currentPage.value--
    fetchData()
  }
}

const nextPage = () => {
  if (currentPage.value < totalPages.value) {
    currentPage.value++
    fetchData()
  }
}

const goToPage = () => {
  if (goToPageNumber.value && goToPageNumber.value >= 1 && goToPageNumber.value <= totalPages.value) {
    currentPage.value = goToPageNumber.value
    fetchData()
  }
}

const goToFishDetails = (id) => {
  router.push({ name: 'FishDetails', params: { id } })
}

onMounted(fetchData)
watch(() => route.params.type, fetchData)
</script>

<style scoped>
.home {
  display: flex;
  flex-direction: column;
  height: 100vh;
}
.home .content{
  flex: 1;
  padding: 1rem;
  overflow-y: auto;
}
.main{
    display: flex;
    justify-content: center;
}
.fish-list {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 20px;
}

.fish-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f0f0f0;
  padding: 10px;
  border-radius: 8px;
  cursor: pointer;
  width: 350px;
  height: 220px;
}
.fish-info{
  /* margin-top: 15px; */
  margin-top: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%; /* 添加这一行来确保fish-info撑满父容器的宽度 */
}
.fish-name {
  font-size: 1rem;
  font-weight: bolder;
  /* color: #fff; */
  /* margin-top: 15px; */
}
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-top: 24px;
}
.fish-image {
    width: 100%;
    height: 150px;
    background-color: #e0e0e0;
    border-radius: 4px;
    margin-bottom: 8px;
    object-fit: cover;
    transition: background-color 0.3s;
  }
</style>